/*
 * Copyright (c) 2017. 深圳掌控网络有限公司. All rights reserved.
 */

package com.yiqi.lottery.feature.home.presenter;

import android.text.TextUtils;
import com.suozhang.framework.component.http.ApiError;
import com.suozhang.framework.component.http.ex.ServerException;
import com.suozhang.framework.entity.bo.UserInfoBo;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.entity.bo.BillingBo;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LuckyHistoryBo;
import com.yiqi.lottery.entity.bo.OrderBo;
import com.yiqi.lottery.feature.home.contract.PayContract;
import com.yiqi.lottery.feature.home.dependencies.DaggerPayPresenterComponent;
import com.yiqi.lottery.feature.lottery.contract.LottoContract;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.DaggerLottoPresenterComponent;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

import javax.inject.Inject;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.List;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2017/4/11 9:31
 */
public class PayPresenter implements PayContract.Presenter {

    private PayContract.View mView;

    @Inject

    PayContract.Model mModel;


    public PayPresenter(PayContract.View mView) {
        this.mView = mView;
        DaggerPayPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void doGetUserInfo() {


        mModel.getUsertInfo().subscribe(new Observer<UserInfoBo>() {
            @Override
            public void onSubscribe(Disposable d) {
//                mView.showLoading(d);
            }

            @Override
            public void onNext(UserInfoBo s) {
//                mView.dismissLoading();
                mView.showUserInfoSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
//                mView.dismissLoading();
                mView.showUserInfoError(e.getMessage());
            }

            @Override
            public void onComplete() {
//                mView.dismissLoading();
            }
        });
    }



    @Override
    public void doCreateOrder(int type, int multiple, String bet_code, String issue, int is_charsing,
                              int charsing_num, int mut_num, int order_amount, String min_reward,
                              String max_reward, int is_follow, int is_open, double safe_rate,
                              String follow_intro) {
        if (multiple < 1) {
            mView.showError("投注倍数不能小于1！");
            return;
        }
        if (TextUtils.isEmpty(bet_code)) {
            mView.showError("投注信息不能为空！");
            return;
        }
        if (TextUtils.isEmpty(issue)) {
            mView.showError("投注期号不能为空！");
            return;
        }


        OrderBo orderBo = new OrderBo(type, multiple, bet_code, issue,
                is_follow, is_charsing, charsing_num, mut_num,
                order_amount, min_reward, max_reward,is_open,safe_rate,follow_intro);

        mModel.createOrder(orderBo).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
                mView.showCreateOrderStart();
            }

            @Override
            public void onNext(String s) {
                mView.showCreateOrderSuccess("创建订单成功！");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage() + "\t\t\t");
                mView.dismissLoading();

            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void orderOptimiz(int order_amount, int type, String issue, String bet_code, String bet_list, String min_reward, String max_reward) {
        if (order_amount < 2) {
            mView.showError("投注金额不能小于2");
            return;
        }
        if (TextUtils.isEmpty(bet_code)) {
            mView.showError("投注信息不能为空！");
            return;
        }
        if (TextUtils.isEmpty(issue)) {
            mView.showError("投注期号不能为空！");
            return;
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("order_amount", order_amount);
        map.put("type", type);
        map.put("issue", issue);
        map.put("bet_code", bet_code);
        map.put("bet_list", bet_list);
        map.put("min_reward", min_reward);
        map.put("max_reward", max_reward);
        mModel.orderOptimiz(map).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.showCreateOrderSuccess("创建订单成功！");
                mView.dismissLoading();
            }

            @Override
            public void onError(Throwable e) {
                mView.showError(e.getMessage() + "\t\t\t");
                mView.dismissLoading();

            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void createCopurch(int type, int multiple, int mut_num, String bet_code, String issue, String amount_fee, String share, String percentage, String subscribe, String guarantee, String declaration, String desc, String finish_time) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("multiple", multiple);
        map.put("mut_num", mut_num);
        map.put("bet_code", bet_code);
        map.put("issue", issue);
        map.put("amount_fee", amount_fee);
        map.put("share", share);
        map.put("percentage", percentage);
        map.put("subscribe", subscribe);
        map.put("guarantee", guarantee);
        map.put("declaration", declaration);
        map.put("desc", desc);
        map.put("finish_time", finish_time);
        mModel.createCopurch(map).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String stringPageData) {
                mView.dismissLoading();
                mView.showCreateOrderSuccess(stringPageData);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void followOrder(Integer orderId, int bet, int order_amount) {
        if (orderId == null) return;
        OrderBo orderBo = new OrderBo(orderId, bet, order_amount);
        mModel.followOrder(orderBo).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showFollowOrderSuccess(s);
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }

    @Override
    public void billingOrder(String order_id, int is_open, double safe_rate, String follow_intro, int is_spread,int is_follow) {
        HashMap<String, String> content=new HashMap<>();
        content.put("is_open",is_open+"");
        content.put("safe_rate",safe_rate+"");
        content.put("follow_intro",follow_intro+"");
        content.put("is_spread",is_spread+"");
        content.put("is_follow",is_follow+"");
        mModel.billingOrder(order_id,content
        ).compose(mView.bindToLife()).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mView.showLoading(d);
            }

            @Override
            public void onNext(String s) {
                mView.dismissLoading();
                mView.showBillingSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissLoading();
                mView.showError(e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.dismissLoading();
            }
        });
    }


}
